package com.goodluck.goodluckim.ui.msg.viewholder;

import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.goodluck.goodluckim.R;
import com.goodluck.goodluckim.db.model.IMMessage;
import com.goodluck.goodluckim.sdk.constant.MsgDirection;


/**
 * Created by wyj on 2018/9/10.
 */

public abstract class MsgViewHolderBase extends BaseItemProvider<IMMessage, BaseViewHolder> {
    private IMMessage mMessage;
    private LinearLayout mMessageBody;
    private FrameLayout mMessageContent;
    private TextView mNameTv;
    private TextView mTimeTv;
    private int viewType;

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    @Override
    public int viewType() {
        return viewType;
    }

    @Override
    public int layout() {
        return R.layout.listitem_message;
    }

    @Override
    public void convert(BaseViewHolder helper, IMMessage data, int position) {
        mMessage = data;
        mMessageBody = helper.getView(R.id.message_content_lly);
        mMessageContent = helper.getView(R.id.message_content_rly);
        mNameTv = helper.getView(R.id.name_tv);
        mTimeTv = helper.getView(R.id.time_tv);
        setTime(helper, data);
        setUserInfo(helper, data);
        setContent(helper, data);
        bindContentView(helper, data, position);
    }

    /**
     * 内容展示区域
     *
     * @return 布局id
     */
    protected abstract int getContentLayoutId();

    /**
     * 将消息数据项与内容的view进行绑定
     *
     * @param helper
     * @param data
     * @param position
     */
    protected abstract void bindContentView(BaseViewHolder helper, IMMessage data, int position);

    /**
     * 中间的气泡
     *
     * @return
     */
    protected int getCenterBackground() {
        return 0;
    }

    /**
     * 左边的气泡
     *
     * @return
     */
    protected int getLeftBackground() {
        return R.drawable.bg_message_left;
    }

    /**
     * 右边的气泡
     *
     * @return
     */
    protected int getRightBackground() {
        return R.drawable.bg_message_right;
    }

    /**
     * 时间设置，满足某种条件显示（条件暂时不知）
     *
     * @param helper
     * @param data
     */
    protected void setTime(BaseViewHolder helper, IMMessage data) {
//        if (){
//            mTimeTv.setVisibility(View.VISIBLE);
//            mTimeTv.setText();
//        } else{
//            mTimeTv.setVisibility(View.GONE);
//            mTimeTv.setText("");
//        }
    }

    /**
     * 控制头像昵称显示隐藏
     *
     * @param helper
     * @param data
     */
    protected void setUserInfo(BaseViewHolder helper, IMMessage data) {
        if (isMiddleItem()) {
            helper.setGone(R.id.left_icon_iv, false);
            helper.setGone(R.id.right_icon_iv, false);
            mNameTv.setVisibility(View.GONE);
            mNameTv.setText("");
        } else {
            if (isReceivedMessage()) {
                helper.setGone(R.id.left_icon_iv, true);
                helper.setGone(R.id.right_icon_iv, false);
                mNameTv.setVisibility(View.VISIBLE);
                mNameTv.setText(data.getObjectName());
            } else {
                helper.setGone(R.id.left_icon_iv, false);
                helper.setGone(R.id.right_icon_iv, true);
                mNameTv.setVisibility(View.GONE);
                mNameTv.setText("");
            }
        }
    }

    /**
     * 消息内容显示
     *
     * @param helper
     * @param data
     */
    protected void setContent(BaseViewHolder helper, IMMessage data) {
        if (mMessageContent.getChildCount() == 0) {
            View.inflate(mContext, getContentLayoutId(), mMessageContent);
        }

        if (mMessageBody.getChildAt(0) != mMessageContent) {
            mMessageBody.removeView(mMessageContent);
            mMessageBody.addView(mMessageContent, 0);
        }

        if (isMiddleItem()) {
            FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams) mMessageBody.getLayoutParams();
            contentParams.gravity = Gravity.CENTER;
            mMessageContent.setBackgroundResource(getCenterBackground());
        } else {
            if (isReceivedMessage()) {
                FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams) mMessageBody.getLayoutParams();
                contentParams.gravity = Gravity.LEFT;
                mMessageContent.setBackgroundResource(getLeftBackground());
            } else {
                FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams) mMessageBody.getLayoutParams();
                contentParams.gravity = Gravity.RIGHT;
                mMessageContent.setBackgroundResource(getRightBackground());
            }
        }
    }

    /**
     * 判断消息方向，是否是接收到的消息
     *
     * @return
     */
    protected boolean isReceivedMessage() {
        return mMessage.getMessageDirection() == MsgDirection.IN;
    }

    /**
     * 该消息是不是居中显示
     *
     * @return
     */
    protected boolean isMiddleItem() {
        return false;
    }
}
